// Copyright 2009 Pigasus.
// GNU GPL v2.
// Author: John Zhang and Jordan Rupprecht.
//
// Defines functions for serializing Pigasus types and unserializing them.
//
// Please NOTE: after you unserialize a list/map/file, make sure you delete that
// pointer when you're done!

#ifndef SERIALIZATION_H_ 
#define SERIALIZATION_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <string>

#include "pigasus-types.h"

using namespace std;

// Number of characters needed to represent a type in serialized form.
const int kTypeAbbreviationLength = 3;
// Number of characters needed to represent a size in serialized form.
const int kFormattedSizeLength = 10;

// Return the first kTypeAbbreviationLength characters of the type.
string AbbreviateType(string type);

// Pad the size with 0's until the string is size kFormattedSizeLength.
string FormatSize(int size);

// Peek ahead in the buffer to see what the next type is.
string GetNextType(string buffer);

// Serialize / unserialize ints.
string Serialize(int v);
int UnserializeInt(string &buffer);

// Serialize / unserialize floats.
string Serialize(double v);
double UnserializeFloat(string &buffer);

// Serialize / unserialize strings.
string Serialize(string v);
string UnserializeString(string &buffer);

// Serialize / unserialize lists.
string Serialize(List &v);
List* UnserializeList(string &buffer);

// Serialize / unserialize maps.
string Serialize(Map &v);
Map* UnserializeMap(string &buffer);

// Serialize / unserialize files.
string Serialize(File &v);
File* UnserializeFile(string &buffer);

#endif  // SERIALIZATION_H_
